{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## sed 常用命令\n",
    "\n",
    "    参考：https://blog.csdn.net/dengjili/article/details/78062066"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     1\t\\S\n",
      "     2\tKernel \\r on an \\m\n"
     ]
    }
   ],
   "source": [
    "# 打印\n",
    "\n",
    "nl /etc/issue | sed -n  '1,2p'\n",
    "\n",
    "# 解释\n",
    "# 1,2 指定行\n",
    "\n",
    "# p 打印的动作\n",
    "\n",
    "# -n 选项默认不输出。(sed 默认的是不匹配的也将输出) -n 选项和p行为时长一起出现。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 替换\n",
    "\n",
    "# 语法:sed ’s#正则#不可用正则表达式#g’  作用替换为空\n",
    "\n",
    "# 如: 更改salt-minion配置文件  /etc/salt/minion  修改master和id\n",
    "\n",
    "# 正常字符之间的替换\n",
    "\n",
    "sed  -i  's/#master: salt/master: 10.59.72.1/g' /etc/salt/minion\n",
    "\n",
    "# 使用系统变量的:\n",
    "\n",
    "sed -i  \"s/#id:/id: `hostname`/g\"   /etc/salt/minion\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 【分组替换】\n",
    "\n",
    "# ()起来的分组，可以使用\\1代替第一个分组，\\n代替第n个分组\n",
    "\n",
    "ifconfig eth0 | sed -n 's#^.*inet addr:\\(.*\\)  Bcast.*$#\\1#gp'\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 【指定行插入】\n",
    "\n",
    "# 首行插入\n",
    "\n",
    "sed -i '1i\\newlineword\\n'  /etc/issue\n",
    "\n",
    "# 指定行后面追加()\n",
    "\n",
    "nl /etc/issue | sed '1,2i\\insert oneline\\n换行符'\n",
    "\n",
    "# 解释:\n",
    "\n",
    "# 1,2 行指定的位置\n",
    "\n",
    "# i 为 插入(在指定位置处插入)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/home/notebook/CookBook/Scripts\n",
      "     1\t/root//root//root//root//root/\n",
      "     2\tWSGI是Python在处理HTTP请求时，规定的一种处理方式。如一个HTTP Request过来了，那么就有一个相应的处理函数来进行处理和返回结果。WSGI就是规定这个处理函数的参数长啥样的，它的返回结果是长啥样的？至于该处理函数的名子和处理逻辑是啥样的，那无所谓。简单而言，WSGI就是规定了处理函数的输入和输出格式。\n"
     ]
    }
   ],
   "source": [
    "# 【删除指定行】\n",
    "\n",
    "# 把第一行第二行删除\n",
    "pwd\n",
    "nl sed_used.txt\n",
    "nl sed_used.txt | sed  '1,2d'  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 删除  #开头的\n",
    "\n",
    "sed  -e '/release/d' -e '/^#/d' file.cnf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 【模式匹配地址】\n",
    "\n",
    "# 匹配地址Addresses的方式有\n",
    "\n",
    "# 1、地址行指定如 \n",
    "\n",
    "#     单行: 1\n",
    "\n",
    "#     多行: 1,2\n",
    "\n",
    "#     $表示最后一行:3,$\n",
    "\n",
    "    addr1,+N: nl /etc/issue | sed -n  '1,+2p'\n",
    "\n",
    "    addr1,~N: nl /etc/passwd | sed -n  '2,~3p'  # 从第二行开始到3的倍数为止\n",
    "\n",
    "    first~step： nl /etc/passwd | sed -n  '2~3p' # 打印first+N*step步长的行\n",
    "\n",
    "# 2、正则匹配\n",
    "#     /regexp/ 正则匹配: \n",
    "    nl /etc/issue | sed -n  '/[cC]ent[oO][sS]/p'\n",
    "     \n",
    "\n",
    "# 3、取反\n",
    "#     不匹配行的取反:\n",
    "    nl /etc/passwd | sed -n  '/nologin/!p'\n",
    "    nl /etc/passwd | sed  '/nologin/!d' \n",
    "\n",
    "    \n",
    "# 实例: 打印奇数行\n",
    "    nl /etc/passwd | sed -n  '1~2p'\n",
    "\n",
    "\n",
    "# 删除 开头的\n",
    "    sed  -e '/release/d' -e '/^#/d' file.cnf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sed: can't read test: No such file or directory\n"
     ]
    },
    {
     "ename": "",
     "evalue": "2",
     "output_type": "error",
     "traceback": []
    }
   ],
   "source": [
    "# 【正则表达式】\n",
    "\n",
    "# 特殊字符意义\n",
    "# &表示引用前面匹配的字符\n",
    "    sed -i 's/root/(&)/g' test                   \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# [被替换的字符串中含有变量]\n",
    "\n",
    "    zone=\"ipo.com\"\n",
    "    echo \"a.txt.ip.cn| \"sed \"s#.$zone.##g\"\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Bash",
   "language": "bash",
   "name": "bash"
  },
  "language_info": {
   "codemirror_mode": "shell",
   "file_extension": ".sh",
   "mimetype": "text/x-sh",
   "name": "bash"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
